﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace ED.EFCore
{
    /// <summary>
    /// EFCore
    /// </summary>
    public static class EFCoreExtensions
	{
		public static IServiceCollection AddEFCore(this IServiceCollection services, Action<EFCoreConfig>? setupAction = null)
		{
			if (setupAction != null)
			{
				services.Configure<EFCoreConfig>(setupAction);
            }
			var config = services.BuildServiceProvider().GetService<IOptions<EFCoreConfig>>();
            // 注入EFCore
            //配置EF的服务注册
            services.AddDbContext<EFCoreDBContext>(
				t => {
                    t.UseSqlServer(config?.Value.SqlConnectionString ?? "");
				});

			return services;
		}
	}
}